<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>老师添加</title>

    <link rel="stylesheet" type="text/css" href="/static/js/layui-v2.5.7/layui/css/layui.css">
    <script type="text/javascript" src="/static/js/layui-v2.5.7/layui/layui.js"></script>
    <script type="text/javascript" src="/static/js/myJs.js"></script>

</head>
<body>
<a href="SelectTeacher" class="layui-btn layui-btn-radius layui-btn-primary">
    <i class="layui-icon">&#xe65a;</i>
</a>
<div class="layui-container">
    <div class="layui-row">
        <form action="InsertTeacher" method="post" class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label" for="name_id">老师名字</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" id="name_id" class="layui-input"
                           placeholder="请输入老师名字" required>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="password_id">老师密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="password" id="password_id" class="layui-input"
                           placeholder="请输入老师密码" required>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="age_id">老师年龄</label>
                <div class="layui-input-inline">
                    <input type="number" name="age" id="age_id" class="layui-input"
                           placeholder="请输入老师年龄" required>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">老师性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="男" class="layui-input" title="男" required>
                    <input type="radio" name="sex" value="女" class="layui-input" title="女" required>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">科目</label>
                <div class="layui-input-block">

                    <div id="check">
                        <!--                           <input type="checkbox" name="c_name[i]" value="1" title="223" class="layui-input">-->
                    </div>

                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" for="create_time_id">时间</label>
                <div class="layui-input-inline">
                    <input type="text" name="create_time" id="create_time_id" class="layui-input"
                           placeholder="请输入时间" required>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" onclick="add();" class="layui-btn">提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>

        </form>
    </div>
</div>


<script type="text/javascript">

    $(function () {
        layui.use(['form','laydate'], function () {
            var form = layui.form;
            window.laydate = layui.laydate;

            datetime();

            $.ajax({
                dataType: "json",
                url: "/studentLayer/Student/course",
                method: "POST",
                success(data) {
                    var html = "";
                    data.data.forEach(function (value, index) {

                        // $("#check").append('<input type="checkbox"  name="c_name" value="'+value.id+'" title="'+value.name+'" class="layui-input">');
                        html = html
                            + '<input type="checkbox"  name="c_name" value="' + value.id + '" title="' + value.name + '" class="layui-input">'

                    })
                    // console.log(html);
                    $("#check").html(html);
                    form.render("checkbox");
                }
            })

        })
    })


    function datetime(){
        window.laydate.render({
            elem:'#create_time_id',
            type:'datetime',
            theme: '#393D49',
            calendar: true
        });

    }




    function add() {
        var count = 0;
        var c_name_array = new Array();
        $("#check>input:checked").each(function (index, element) {
            c_name_array.push(element.value)
            count++;
        });
        console.log(c_name_array);
        $.ajax({
            dataType: "json",
            url: "/studentLayer/Teacher/InsertTeacher",
            method: "POST",
            data: {
                "name": $("#name_id").val(),
                "password": $("#password_id").val(),
                "age": $("#age_id").val(),
                "sex": $("input[name=sex]:checked").val(),
                "c_name": c_name_array,
                "create_time": $("#create_time_id").val(),
            },

            success: function (data) {
                layer.msg("laj");
                if (data.code == 0) {
                    layer.msg(data.msg, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.location.reload(index);
                        parent.layer.close(index);
                        window.location.href = "/studentLayer/Teacher/SelectTeacher";
                    });
                } else {
                    layer.msg("添加失败", function () {

                        var index = parent.layer.getFrameIndex(window.name);
                        parent.location.reload(index);
                        parent.layer.close(index);
                        window.location.href = "/studentLayer/Teacher/SelectTeacher";
                    });

                }
            }

        })
    }


</script>


</body>
</html>